using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Qy.Core;
using Qy.Web.Authorize;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Qy.Web.Areas.Api;

[Route("Api/[controller]/[action]")]
[Verify]
public class ServerController : Base
{
    private readonly IWebHostEnvironment _hostEnvironment;
    public ServerController(IWebHostEnvironment hostEnvironment)
    {
        _hostEnvironment = hostEnvironment;
    }

    [HttpGet]
    public IActionResult Server()
    {
        //核心数
        int cpuNum = Environment.ProcessorCount;
        string computerName = Environment.MachineName;
        string osName = RuntimeInformation.OSDescription;
        string osArch = RuntimeInformation.OSArchitecture.ToString();
        string version = RuntimeInformation.FrameworkDescription;
        string appRAM = ((double)Process.GetCurrentProcess().WorkingSet64 / 1048576).ToString("N2") + " MB";
        string startTime = Process.GetCurrentProcess().StartTime.ToString("yyyy-MM-dd HH:mm:ss");
        string sysRunTime = ServerUtility.GetRunTime();
        string serverIP = Request.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString() + ":" + Request.HttpContext.Connection.LocalPort;//获取服务器IP

        var programStartTime = Process.GetCurrentProcess().StartTime;
        string programRunTime = DateUtility.FormatTime((DateTime.Now - programStartTime).TotalMilliseconds.ToString().Split('.')[0].ParseToLong());
        var data = new
        {
            cpu = ServerUtility.GetComputerInfo(),
            disk = ServerUtility.GetDiskInfos(),
            sys = new { cpuNum, computerName, osName, osArch, serverIP, runTime = sysRunTime },
            app = new
            {
                name = _hostEnvironment.EnvironmentName,
                rootPath = _hostEnvironment.ContentRootPath,
                webRootPath = _hostEnvironment.WebRootPath,
                version,
                appRAM,
                startTime,
                runTime = programRunTime,
                host = serverIP
            },
        };

        return Success(data);
    }
}
